
****************************************************
* Market clearing replication
****************************************************


** Need to install these packages

capture ado uninstall reghdfe
capture ado uninstall ftools
capture ado uninstall ivreg2

ssc install ftools, replace
ssc install reghdfe, replace
ssc install ivreg2, replace

capture ssc install ivreghdfe 
capture ssc install ranktest

mata: mata clear
clear all




* ============================================================================ *
* 
* SET DUMMY DATA GLOBAL PARAMETER
* 	If set to 1, uses dummy data. Used for publicly available replication file 
* 	If set to 0, uses actual raw data used for actual analysis in the paper
*
* ============================================================================ *
global dummy=1

local marco=0
if `marco'==1{
    global homepath="C:/Users/mcsam/Dropbox/market_clearing_replication_clean"
    *global homepath="C:/Users/Marco Sammon/Dropbox/market_clearing_replication_clean"
    *global homepath="C:/Users/msammon/Dropbox/market_clearing_replication_clean"

    global path="$homepath"
    global adopath="$homepath/ado_files"
    global CLEAN="$homepath/code_data_cleaning"
    global PROD="$homepath/code_analysis"

    if $dummy==1{
        global rawpath="$homepath/data_raw_dummy"
        global temppath="$homepath/data_temp_dummy"
        global cleanpath="$homepath/data_clean_dummy"
        global outpath="$homepath/output_dummy"
    }
    else{
        global rawpath="$homepath/data_raw"
        global temppath="$homepath/data_temp"
        global cleanpath="$homepath/data_clean"
        global outpath="$homepath/output"
    }
}
else{
    global homepath="/Users/jshim2/Dropbox/projects/market_clearing_replication_clean"

    global path="$homepath"
    global adopath="$homepath/ado_files"
    global CLEAN="$homepath/code_data_cleaning"
    global PROD="$homepath/code_analysis"

    if $dummy==1{
        global rawpath="$homepath/data_raw_dummy"
        global temppath="$homepath/data_temp_dummy"
        global cleanpath="$homepath/data_clean_dummy"
        global outpath="$homepath/output_dummy"
    }
    else{
        global rawpath="$homepath/data_raw"
        global temppath="$homepath/data_temp"
        global cleanpath="$homepath/data_clean"
        global outpath="$homepath/output"
    }
}

timer clear

****************************************************
* DATA CLEANING CODE
****************************************************

timer on 1
include "${CLEAN}/1_clean_crsp_daily.do"
timer off 1

timer on 2
include "${CLEAN}/2_identify_passive_funds.do"
timer off 2

timer on 3
include "${CLEAN}/3_merge_s12_crsp.do"
timer off 3

timer on 4
include "${CLEAN}/3b_clean_crsp_mf_data.do"
timer off 4

timer on 5
include "${CLEAN}/4_aggregate_s12_data.do"
timer off 5

timer on 6
include "${CLEAN}/5_stock_level_cret.do"
timer off 6

timer on 7
include "${CLEAN}/6_assign_industries.do"
timer off 7

timer on 8
include "${CLEAN}/7_prep_13f_data.do"
timer off 8

timer on 9
include "${CLEAN}/8_aggregate_13f_data.do"
timer off 9

timer on 10
include "${CLEAN}/9_clean_short_interest_data.do"
timer off 10

timer on 11
include "${CLEAN}/10_clean_insiders_data.do"
timer off 11

timer on 12
include "${CLEAN}/11_index_membership_float_adjustments.do"
timer off 12

timer on 13
include "${CLEAN}/11b_clean_monthly_russell_data.do"
timer off 13

timer on 14
include "${CLEAN}/12_create_investor_groups.do"
timer off 14

timer on 15
include "${CLEAN}/12b_create_investor_groups_broadsys.do"
timer off 15

timer on 16
include "${CLEAN}/13_flow_iv.do"
timer off 16

timer on 17
include "${CLEAN}/14_decomposition_seo_bb_comp.do"
timer off 17

timer on 18
include "${CLEAN}/15_compustat_compensation_bbs.do"
timer off 18

timer on 19
include "${CLEAN}/15b_compustat_compensation_bbs_annual.do"
timer off 19

timer on 20
include "${CLEAN}/16_clean_rsu_psu_data.do"
timer off 20

timer on 21
include "${CLEAN}/17_identify_broad_sys_demand.do"
timer off 21

timer on 22
include "${CLEAN}/18_s12_matchquality.do"
timer off 22

timer on 23
include "${CLEAN}/19_clean_crsp_mf_data.do"
timer off 23

timer on 24
include "${CLEAN}/20_var_decomp_data.do"
timer off 24

****************************************************
* ANALYSIS CODE
****************************************************

timer on 25
include "${PROD}/1_bin_scatter.do"
timer off 25

timer on 26
include "${PROD}/2_ols_regression.do"
timer off 26

timer on 27
include "${PROD}/3_horizon.do"
timer off 27

timer on 28
include "${PROD}/4a_aum_by_type.do"
timer off 28

timer on 29
include "${PROD}/4b_compare_weights_mkt_weights.do"
timer off 29

timer on 30
include "${PROD}/4c_compare_turnover.do"
timer off 30

timer on 31
include "${PROD}/4d_var_decomp.do"
timer off 31

timer on 32
include "${PROD}/5_iv_baseline.do"
timer off 32

timer on 33
include "${PROD}/5b_iv_bygroup.do"
timer off 33

timer on 34
include "${PROD}/6_ols_regression_heterogenity.do"
timer off 34

timer on 35
include "${PROD}/7_decomposition_seo_bb_comp.do"
timer off 35

timer on 36
include "${PROD}/8_compensation_magnitudes.do"
timer off 36

timer on 37
include "${PROD}/9_rsu_psu_magnitudes.do"
timer off 37

****************************************************
* TIMING RESULTS
****************************************************

timer list
